package com.sttk.secreconeclick.bookkeep.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.ManageTypeAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityManageTypeBinding;
import com.sttk.secreconeclick.bookkeep.dialog.SelAccountBookDialog;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;
import com.sttk.secreconeclick.bookkeep.utils.TypeDragCallback;

import java.util.ArrayList;
import java.util.List;

public class ManageTypeActivity extends BaseAc<ActivityManageTypeBinding> implements View.OnClickListener {

    public static int accountBookPos;
    private ManageTypeAdapter manageTypeAdapter;
    private List<AccountBookBean> listSave = new ArrayList<>();
    private AccountBookBean accountBookBean;
    private boolean isIncome;

    @Override
    protected int onCreate() {
        return R.layout.activity_manage_type;
    }

    @Override
    protected void initView() {
        mDataBinding.ivManageTypeBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvExpenditure.setOnClickListener(this);
        mDataBinding.tvIncome.setOnClickListener(this);
        mDataBinding.tvImport.setOnClickListener(this);
        mDataBinding.tvAdd.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext));
        manageTypeAdapter = new ManageTypeAdapter();
        mDataBinding.rvTypeList.setAdapter(manageTypeAdapter);

        // 绑定拖动功能
        ItemTouchHelper touchHelper = new ItemTouchHelper(new TypeDragCallback(manageTypeAdapter));
        touchHelper.attachToRecyclerView(mDataBinding.rvTypeList);

        manageTypeAdapter.setListener(new ManageTypeAdapter.Ilistener() {
            @Override
            public void clickEdit(int pos) {
                AddTypeActivity.accountPos = SPUtil.getInt(mContext, Content.accountBook, 0);
                AddTypeActivity.isIncome = isIncome;
                AddTypeActivity.selPosition = pos;
                Intent intent = new Intent(ManageTypeActivity.this, AddTypeActivity.class);
                startActivityForResult(intent, Content.ADD_FLAG);
            }

            @Override
            public void clickDelete(int pos) {
                if (isIncome) {
                    accountBookBean.getIncomeTypeList().remove(pos);
                } else {
                    accountBookBean.getExpenditureTypeList().remove(pos);
                }
                manageTypeAdapter.notifyDataSetChanged();
                AppSpUtils.saveAccountBookList(listSave);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvExpenditure) {//支出
            isIncome = false;
            mDataBinding.tvExpenditure.setBackgroundResource(R.drawable.expenditure_sel);
            mDataBinding.tvIncome.setBackgroundResource(0);
            setData();
        } else if (id == R.id.tvIncome) {//收入
            isIncome = true;
            mDataBinding.tvExpenditure.setBackgroundResource(0);
            mDataBinding.tvIncome.setBackgroundResource(R.drawable.income_sel);
            setData();
        } else if (id == R.id.tvImport) {//导入
            showSelAccountBookDialog();
        } else if (id == R.id.tvAdd) {//新增类别
            AddTypeActivity.accountPos = accountBookPos;
            AddTypeActivity.isIncome = true;
            AddTypeActivity.selPosition = -1;
            Intent intent = new Intent(this, AddTypeActivity.class);
            startActivityForResult(intent, Content.ADD_FLAG);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        setData();
    }

    private void getData() {
        listSave = AppSpUtils.getAccountBookList();
        accountBookBean = listSave.get(accountBookPos);
        if (listSave.size() == 1) {
            mDataBinding.tvImport.setVisibility(View.GONE);
        } else {
            mDataBinding.tvImport.setVisibility(View.VISIBLE);
        }
    }

    private void setData() {
        if (isIncome) {
            if (accountBookBean.getIncomeTypeList() != null && accountBookBean.getIncomeTypeList().size() > 0) {
                manageTypeAdapter.setList(accountBookBean.getIncomeTypeList());
                mDataBinding.rvTypeList.setVisibility(View.VISIBLE);
                mDataBinding.tvTypeNoData.setVisibility(View.GONE);
            } else {
                mDataBinding.rvTypeList.setVisibility(View.GONE);
                mDataBinding.tvTypeNoData.setVisibility(View.VISIBLE);
            }
        } else {
            if (accountBookBean.getExpenditureTypeList() != null && accountBookBean.getExpenditureTypeList().size() > 0) {
                manageTypeAdapter.setList(accountBookBean.getExpenditureTypeList());
                mDataBinding.rvTypeList.setVisibility(View.VISIBLE);
                mDataBinding.tvTypeNoData.setVisibility(View.GONE);
            } else {
                mDataBinding.rvTypeList.setVisibility(View.GONE);
                mDataBinding.tvTypeNoData.setVisibility(View.VISIBLE);
            }
            manageTypeAdapter.setList(accountBookBean.getExpenditureTypeList());
        }
    }

    private void showSelAccountBookDialog() {
        SelAccountBookDialog dialog = new SelAccountBookDialog(mContext);
        dialog.setSelPosition(accountBookPos);
        dialog.setListener(new SelAccountBookDialog.Ilistener() {
            @Override
            public void clickItem(int pos) {
                if (pos >= accountBookPos) pos++;
                SelTypeActivity.selPosition = pos;
                startActivity(SelTypeActivity.class);
            }
        });
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Content.ADD_FLAG && resultCode == RESULT_OK) {

        }
    }
}